/*******************************************************************************
 * Copyright (c) 2014-8-18 @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a>.
 * All rights reserved.
 *
 * Contributors:
 *     <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a> - initial API and implementation
 ******************************************************************************/
package com.foreveross.project.projectonline.model;

import java.util.Date;

import org.iff.infra.util.Assert;
import org.iff.infra.util.StringHelper;

import com.jfinal.plugin.activerecord.Model;

/**
 * @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a> 
 * @since 2014-8-18
 * <pre>
	-- ----------------------------
	-- Table structure for `TP_MGT_USERROLEROLE`
	-- ----------------------------
	DROP TABLE IF EXISTS `TP_MGT_USERROLEROLE`;
	CREATE TABLE `TP_MGT_USERROLEROLE` (
	  `ID`          varchar(50) NOT NULL COMMENT '主键',
	  `USER_ID`     varchar(50) NOT NULL COMMENT '用户ID',
	  `ROLE_ID`     varchar(50) NOT NULL COMMENT '角色ID',
	  `UPDATE_TIME` datetime    NOT NULL COMMENT '更新时间',
	  `CREATE_TIME` datetime    NOT NULL COMMENT '创建时间',
	  `IS_ENABLE`   char(1)     NOT NULL DEFAULT 'Y' COMMENT '是否有效',
	  PRIMARY KEY (`ID`),
	  KEY `INDEX_TP_MGT_USERROLEROLE_USER_ID`     (`USER_ID`),
	  KEY `INDEX_TP_MGT_USERROLEROLE_ROLE_ID`     (`ROLE_ID`),
	  KEY `INDEX_TP_MGT_USERROLEROLE_UPDATE_TIME` (`UPDATE_TIME`),
	  KEY `INDEX_TP_MGT_USERROLEROLE_CREATE_TIME` (`CREATE_TIME`),
	  KEY `INDEX_TP_MGT_USERROLEROLE_IS_ENABLE`   (`IS_ENABLE`)
	) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户角色表';
 * </pre> 
 */
@SuppressWarnings("serial")
public class TpMgtUserRole extends Model<TpMgtUserRole> {
	public static final TpMgtUserRole dao = new TpMgtUserRole();

	public boolean save() {
		set("ID", StringHelper.uuid());
		set("UPDATE_TIME", new Date());
		set("CREATE_TIME", get("UPDATE_TIME"));
		validate(true);
		return super.save();
	}

	public boolean update() {
		remove("CREATE_TIME");
		set("UPDATE_TIME", new Date());
		validate(false);
		return super.update();
	}

	public boolean disable() {
		String id = get("ID");
		clear();
		set("ID", id);
		set("IS_ENABLE", "N");
		return super.update();
	}

	public void validate(boolean isCreate) {
		Assert.notEmpty((String) get("ID"), "[TP_MGT_USERROLE_ID][IS_EMPTY]");
		if (isCreate) {
			Assert.notEmpty((String) get("USER_ID"), "[TP_MGT_USERROLE_USER_ID][IS_EMPTY]");
			Assert.notEmpty((String) get("ROLE_ID"), "[TP_MGT_USERROLE_ROLE_ID][IS_EMPTY]");
			if (!"N".equals(get("IS_ENABLE"))) {
				set("IS_ENABLE", "Y");
			} else {
				set("IS_ENABLE", "N");
			}
		}
	}
}
